home *** CD-ROM | disk | FTP | other *** search
- Path: news.production.compuserve.com!news
- From: John Puopolo <102262.612@CompuServe.COM>
- Newsgroups: comp.lang.c++
- Subject: Re: Dynamic Array Allocation With new/delete
- Date: 15 Feb 1996 22:49:14 GMT
- Organization: Lotus Development Corporation
- Message-ID: <4g0d9a$3h6$1@mhadg.production.compuserve.com>
- References: <+4bExc9nXQoO083yn@mbnet.mb.ca>
-
- Nathan...One method to do what you want is to use "placement
- new." This consists of allocating a buffer (using operator new)
- that can hold the things you want. You then create the objects
- "in" the buffer.
-
- For example, let's suppose I want to allocate enough memory to
- hold 10 Radio objects....
-
- int main() {
- // alloc block of memory large enough to hold 10 Radios
- char* buf = new unsigned char[sizeof(Radio) * 10];
-
- // alloc a single Radio object in the buffer
- Radio* pRadio = new (buf) Radio;
- }
- This says "create a Radio object at memory location buf." To
- create the next Radio...
-
- Radio* pRadio2 = new (buf + sizeof(Radio)) Radio; // ...etc.
-
- NOTE: Must use placement delete: i.e., must delete via
- pRadio2->~Radio()...true for any object created via
- placement new.
-
- Hope this helps... John
-
- --
- John Puopolo, Lotus Development Corporation
- 102262,612 or jpuopolo@crd.lotus.com
- "Knowledge itself is power."
-